package yznu_Bean_handle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import Goods_Utils.Good;
import Order_Utils.Order;
import User_Utils.User;
import yznu_jdbc.JDpoor;
import yznu_utils.Store;


public class Query {

	Connection con = new JDpoor().CreateCon();
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public Good Query_good(String Username) {
		String condition = "select * from goods_info where name=?";
		Good good = new Good();
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, Username);
	    	rs=pre.executeQuery(); 
	    	while(rs.next()){
	    		good.setName(rs.getString("name"));
				good.setPrice(rs.getString("price"));
				good.setType(rs.getString("type"));
				good.setUrl(rs.getString("url"));
				good.setStock(rs.getString("stock"));
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		
		return good;
	}
	
	public User Query_user(String Username) {
		String condition = "select * from user_info where name=?";
		User user = new User();
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, Username);
	    	rs=pre.executeQuery(); 
	    	while(rs.next()){
	    		user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setEmail(rs.getString("email"));
				user.setPassword(rs.getString("password"));
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		
		return user;
	}

	public ArrayList<Order> Query_order(String Username) {
		// TODO Auto-generated method stub
		String condition = "select * from order_info where user=?";
		ArrayList<Order> orders = new ArrayList<Order>();
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, Username);
	    	rs=pre.executeQuery(); 
	    	while(rs.next()){
	    		Order order = new Order();
	    		order.setName(rs.getString("name"));
	    		order.setNum(rs.getString("num"));
	    		order.setPrice(rs.getString("price"));
	    		order.setTotal(rs.getString("total"));
	    		order.setUser(rs.getString("user"));
	    		orders.add(order);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(Username+"订单查询出错："+e);
		}
		
		return orders;
	}

	public String Query_perTotal(String username) {
		// TODO Auto-generated method stub
		String total = "0";
		try {
			sql = con.createStatement();
			rs=sql.executeQuery("select sum(total)total,user from order_info where user='"+username+"'");
			while(rs.next()){
	    		total = rs.getString("total");
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return total;
	}

	public String Query_others_perTotal(String username) {
		// TODO Auto-generated method stub
		String total = "0";
		try {
			sql = con.createStatement();
			rs=sql.executeQuery("select sum(total)total,user from others_order_info where user='"+username+"'");
			while(rs.next()){
	    		total = rs.getString("total");
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return total;
	}

	public ArrayList<Order> Query_others_order(String username) {
		// TODO Auto-generated method stub
		String condition = "select * from others_order_info where user=?";
		ArrayList<Order> orders = new ArrayList<Order>();
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, username);
	    	rs=pre.executeQuery(); 
	    	while(rs.next()){
	    		Order order = new Order();
	    		order.setName(rs.getString("name"));
	    		order.setNum(rs.getString("num"));
	    		order.setPrice(rs.getString("price"));
	    		order.setTotal(rs.getString("total"));
	    		order.setUser(rs.getString("user"));
	    		orders.add(order);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(username+"订单查询出错："+e);
		}
		
		return orders;
	}

	public Store Query_store(String username) {
		// TODO Auto-generated method stub
		String condition = "select * from otherStore_info where name=?";
		Store store = new Store();
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, username);
	    	rs=pre.executeQuery(); 
	    	while(rs.next()){
	    		store.setName(rs.getString("name"));
	    		store.setAddress(rs.getString("address"));
	    		store.setPhone(rs.getString("phone"));
	    		store.setBoss(rs.getString("boss"));
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		
		return store;
	}
	
	
}
